import java.awt.List;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.aliasi.tokenizer.EnglishStopTokenizerFactory;
import com.aliasi.tokenizer.IndoEuropeanTokenizerFactory;
import com.aliasi.tokenizer.LowerCaseTokenizerFactory;
import com.aliasi.tokenizer.NGramTokenizerFactory;
import com.aliasi.tokenizer.PorterStemmerTokenizerFactory;
import com.aliasi.tokenizer.Tokenizer;
import com.aliasi.tokenizer.TokenizerFactory;



/*@Sangeeta
 * A class to contain general list of function, can be used by many classes.
 * 1.Clean 
 **/

public class general_fun
{
	IndoEuropeanTokenizerFactory ietf = IndoEuropeanTokenizerFactory.INSTANCE;
	TokenizerFactory f2=new LowerCaseTokenizerFactory(ietf);
	EnglishStopTokenizerFactory tokFact=new EnglishStopTokenizerFactory(f2); 
   
	PorterStemmerTokenizerFactory stemFact = new PorterStemmerTokenizerFactory(ietf) ; 
    
	
	//remove this words if they match exactly
	public String s_w1[]={        "public","static","void","java","svn","i","$","may", 
			                      "please","forum","refer","like","done","code","need","needs","away","using",
			                      "etc","then","should","now","even","though","long","ago","something",
			                      "various","same","both","branch","branches","trunk","just","another","see","via",
			                      "whether","bugs","bug","too","due","cannot","why","&amp",
			                      "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s",
			                      "t","u","v","w","x","y","z",
			                      "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S",
			                      "T","U","V","W","X","Y","Z",
			                      "0","1","2","3","4","5","6","7","8","9"};
	
	
	//remove these words even if they got partial match
	
	public String partial_words[]={"branches","branch","Branch","trunk","JBoss"};
	
	
	
	
//This method will remove general stop words from
public String remove_stop_words(String input)
{
		String output="";
		char temp[]=input.toCharArray();
		Tokenizer tokenizer = tokFact.tokenizer(temp,0,temp.length);
		
		
		//int i=0;
		for(String itr:tokenizer)
		{
			//System.out.println(i+"="+itr);
			output+=itr+" ";
			//i++;
		}

		
		return output;
			
}//remove
	

// Applying stemming on words, used to stem camel cased words for baseline approach
public String apply_stemming(String input) 
{
  String output = "";
  char temp[] = input.toCharArray();
  Tokenizer tokenizer = stemFact.tokenizer(temp, 0, temp.length);
  for(String itr:tokenizer )
  {
	output = output + itr+" ";  
  }
  
  return output;
}
	
	
public String remove_SW(String input) 
{
	//System.out.println("Input="+input); 
	
	String output="";
	 
	 String t[]=input.split("\\s+");
	 java.util.List<String> list=Arrays.asList(t);
	 Vector<String> temp=new Vector<String>(list);
	 
	 for(int i=0;i<s_w1.length;i++)
	  {
		 for(int j=0;j<temp.size();j++)
		 {
			 if(s_w1[i].equalsIgnoreCase(temp.get(j).toLowerCase()))
			 {
				temp.remove(j); 
				j--;
				
			 }//if
		 }//for
	  }//for
	 
	 for(int i=0;i<temp.size();i++)
	 {
		 output+=temp.get(i)+" ";
	 }
	 
	 //System.out.println("Output="+output);
	 
	 return output;
	 
}//function


/*
public String remove_path_stop_words(String file_m_path) 
{
	String file_path_arr[] = file_m_path.split("\\s+");
	Vector<String> file_path_v= new Vector<String>();
	
	for(int i=0;i<file_path_arr.length;i++)
		file_path_v.add(file_path_arr[i]);
	
  for(int i=0;i<file_path_v.size();i++)
  {
	  String  cw = file_path_v.get(i);
	  
	  for(int j = 0;j<partial_words.length;j++)
	  {
		  if(cw.contains(partial_words[j]))
		  {
			  file_path_v.remove(i);
			  i--;
		  }
	  }
  }
  
  file_m_path="";
  for(int i=0;i<file_path_v.size();i++)
  {
	  file_m_path =  file_m_path  +" " +file_path_v.get(i);
  }
  
  return file_m_path;
	
}//
*/

public String remove_slash_and_file_name(String file_path) 
{
 String new_file_path="";
 
 String temp[]=file_path.split("/");
 
 for(int i=0;i<temp.length-1;i++)
  {
	 new_file_path+=temp[i]+" "; 
  }
 
 return new_file_path; 
}
/*
public String remove_one_letter(String content)
{
	String pattern = "(\\s+[a-zA-Z]\\s+)";
    Pattern r = Pattern.compile(pattern);
  
	Matcher m1 = r.matcher(content);
    while(m1.find()) {
    	String s = m1.group(0);
    	//System.out.println(s);
    	content=content.replaceAll(content," ");
    	m1=r.matcher(content);
    	//System.out.println(s);
    	//list.add(s);
    } 

	//for (int i=0;i<list.size();i++)
		//cleanContent=cleanContent.replace(list.get(i), " ");
    
    return content;
}
*/
public String clean(String content)
	{
			String cleanContent=content;
			//1. Remove stop words
			//2. Remove special chars . : -	( ) [ ] ? * = ^ % $ #
			
			cleanContent=cleanContent.replaceAll("~", " ");
			cleanContent=cleanContent.replaceAll("`", " ");
			cleanContent=cleanContent.replaceAll("!", " ");
			cleanContent=cleanContent.replaceAll("@", " ");
			cleanContent=cleanContent.replaceAll("#", " ");
			//cleanContent=cleanContent.replaceAll("\\$", " ");
			cleanContent=cleanContent.replaceAll("%", " ");
			cleanContent=cleanContent.replaceAll("\\^", " ");
			cleanContent=cleanContent.replaceAll("&amp"," ");
			cleanContent=cleanContent.replaceAll("&", " ");
			cleanContent=cleanContent.replaceAll("\\*", " ");
			cleanContent=cleanContent.replaceAll("\\(", " ");
			cleanContent=cleanContent.replaceAll("\\)", " ");
			cleanContent=cleanContent.replaceAll("_", " ");
			cleanContent=cleanContent.replaceAll("-", " ");
			cleanContent=cleanContent.replaceAll("\\+", " ");
			cleanContent=cleanContent.replaceAll("=", " ");
			cleanContent=cleanContent.replaceAll("<", " ");
			cleanContent=cleanContent.replaceAll(">", " ");
			cleanContent=cleanContent.replaceAll("\\?", " ");
			cleanContent=cleanContent.replaceAll("\\.", " ");
			cleanContent=cleanContent.replaceAll(",", " ");
			cleanContent=cleanContent.replaceAll("/", " ");
			cleanContent=cleanContent.replaceAll(";", " ");
			cleanContent=cleanContent.replaceAll(":", " ");
			cleanContent=cleanContent.replaceAll("'", " ");
			cleanContent=cleanContent.replaceAll("\"", " ");
			cleanContent=cleanContent.replaceAll("\\{", " ");
			cleanContent=cleanContent.replaceAll("\\[", " ");
			cleanContent=cleanContent.replaceAll("}", " ");
			cleanContent=cleanContent.replaceAll("]", " ");
			cleanContent=cleanContent.replaceAll("\\|", " ");
			cleanContent=cleanContent.replaceAll("\\\\", " ");
		
			
			
			//replace digit, Not a comment BTW 
			/*
			String pattern = "(\\d+)";
		    Pattern r = Pattern.compile(pattern);
		    Matcher m = r.matcher(cleanContent);
		    ArrayList<String> list=new ArrayList<String>();
		    while(m.find()) {
		    	String s = m.group(0);
		    	//System.out.println(s);
		    	cleanContent=cleanContent.replaceAll(s," ");
		    	m=r.matcher(cleanContent);
		    	//list.add(s);
		    } 
             */
		
		    /*pattern = "(\\s+[a-zA-Z]\\s+ )";
		    r = Pattern.compile(pattern);
		    m = r.matcher(cleanContent);
		    while(m.find()) {
		    	String s = m.group(0);
		    	System.out.println(s);
		    	cleanContent=cleanContent.replaceAll(s," ");
		    	m=r.matcher(cleanContent);
		    	System.out.println(s);
		    	//ProbCalculation.intr();
		    	//list.add(s);
		    } */
		    
		    cleanContent = cleanContent.replaceAll("\\s+", " ");
		    cleanContent =  cleanContent.replaceAll("\\s+[a-zA-Z]\\s+", " ");
		    cleanContent = cleanContent.replaceAll("\\s+", " ");

			//for (int i=0;i<list.size();i++)
		    //cleanContent=cleanContent.replace(list.get(i), " ");
		
		    
			return cleanContent;			
}

public String[] ngram(String inputText, int start, int end)
{
		String[] result =new String[0];
		
		//start =3
		//end  = 10
		NGramTokenizerFactory ngtf =new NGramTokenizerFactory(start,end);
		Tokenizer tokenizer = ngtf.tokenizer(inputText.toCharArray(), 0, inputText.length());

		Iterator<String> iterator = tokenizer.iterator();

		result = tokenizer.tokenize();

		return result;
}


//To perform camel casing
public String do_camel_casing(String input_str) {
    Pattern pattern = Pattern.compile("([A-Z]|[a-z])[a-z]*");

    Vector<String> tokens = new Vector<String>();
    java.util.regex.Matcher matcher = pattern.matcher(input_str);
    String acronym = "";
    while(matcher.find()) {
        String found = matcher.group();
        if(found.matches("^[A-Z]$")) {
            acronym += found;
        } else {
            if(acronym.length() > 0) {
                //we have an acronym to add before we continue
                tokens.add(acronym);
                acronym  = "";
            }
            tokens.add(found.toLowerCase());
        }
    }
    if(acronym.length() > 0) {
        tokens.add(acronym);
    }
    if (tokens.size() > 0) {
        String humanisedString = make_first_letter_capital(tokens.remove(0));
        for (String s : tokens) {
            humanisedString +=  " " + s;
        }
        return humanisedString;
    }

    return input_str;
}


//make the first letter capital
public String make_first_letter_capital(String input)
{
	String first_cap = input.substring(0, 1).toUpperCase() +input.substring(1);
	
	return first_cap;
}


//main
public static void main(String args[])
{
		general_fun gf= new general_fun();
		String a="the shfjd the a b c fgd tsdfsg \n hfgjf th hi which why helo";
	    gf.remove_stop_words(a);
		
}//main


//Used to insert to 
public void insert_to_td_sim(ArrayList<td_fpath_object> td, Connection conn,String score_table) 
{
	 Statement stmt =  null;
	    
	    for(int i=0;i<td.size();i++)
	    {
	      
	    	String bugid = td.get(i).bugid;
	    	String fid = td.get(i).fid;
	    	String table = td.get(i).table;
	    	String title = td.get(i).title ;
	    	//String desc =  td.get(i).desc;    	
	    	String file_name = td.get(i).file_name;
	    	String file_path= td.get(i).file_path;
	    	double tf_sim =td.get(i).tf_sim;
	    	double tpath_sim = td.get(i).t_path_sim;
	    	double df_sim =td.get(i).df_sim;
	    	double dpath_sim = td.get(i).d_path_sim;
	    	
	    	double sum1=tf_sim+tpath_sim+df_sim+dpath_sim;
	    	//double sum2=tf_sim+tpath_sim;
	    	//double sum3=df_sim+dpath_sim;
	    	//double sum4=tf_sim+df_sim;
	    	//double sum5=tpath_sim+dpath_sim;
	    	
	    	//double sum = tf_sim+tpath_sim;
	    	
	    	
	    	/*String insert_q = "insert into "+ score_table +" values('"+bugid+"','"+fid+"','"+table+"','"+title+"','"+desc+"','"+
	    	file_name+"','"+file_path+"',"+tf_sim+","+tpath_sim+","+df_sim+","+dpath_sim+","+sum1+","+sum2+","+
	    	sum3+","+sum4+","+sum5+")";*/
	    	
	    	String insert_q = "insert into "+ score_table +" values('"+bugid+"','"+fid+"','"+table+"','"+title+"','"+
	    	file_name+"','"+file_path+"',"+tf_sim+","+tpath_sim+","+df_sim+","+dpath_sim+","+sum1 +")";
	    	
	        
	    	try 
	    		{
	    			stmt =  conn.createStatement();
	    			stmt.executeUpdate(insert_q);
			
	    		} catch (SQLException e) 
	    			{	e.printStackTrace();}
	    }//for
		
	    try
	    {
		 if(stmt!=null)
			stmt.close();
	    }catch(SQLException e)
	    {e.printStackTrace();}
		
	
}

	
}//class
